// pages/sqmd/sqmd.js
import {
  mobileValidate
} from '../../utils/util'
import {
  addressAdd,
  addressUpdate,
  addressInfo,
  addressDelete
} from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    type: '',
    consignee: '',
    region: [],
    address: '',
    phone: '',
    isDefault: false,
    disabled: false,
    consigneeTip: false,
    phoneTip: false,
    regionTip: false,
    addressTip: false,
    id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      title: options.type == 'new' ? '新增地址' : '编辑地址',
      type: options.type,
      id: options.id || ''
    })
    if (options.type == 'update') {
      this.getInfo(options.id)
    }
  },

  getInfo(id) {
    addressInfo(id).then(res => {
      const {
        consignee,
        phone,
        address,
        isDefault,
        provinceName,
        cityName,
        areaName
      } = res
      this.setData({
        consignee,
        phone,
        address,
        isDefault: isDefault == 1,
        region: [provinceName, cityName, areaName]
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handleInput(e) {
    let value = e.detail.value
    let type = e.currentTarget.dataset.type
    this.setData({
      [type]: value,
      [type + 'Tip']: false
    })
    if (type == 'phone') {
      if (!mobileValidate(value)) {
        this.setData({
          phoneTip: true
        })
      }
    }
  },
  bindRegionChange(e) {
    this.setData({
      region: e.detail.value,
      regionTip: false
    })
  },
  bindLocationChange(e) {
    const that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.showToast({ //这里提示失败原因
                title: '授权成功！',
                duration: 1500
              })
            },
            fail() {
              wx.showToast({
                title: '需要授权位置信息',
                icon: 'none'
              })
            }
          })
        } else {
          wx.chooseLocation({
            success(res) {
              console.log(res);
              that.setData({
                point: res.longitude + ',' + res.latitude,
                address: res.address,
                areaTip: false
              })
            },
            fail(res) {
              console.log(res);
            }
          })
        }
      }
    })
  },
  bindStoreChange(e) {
    this.setData({
      storeId: this.data.storeList[e.detail.value].id,
      storeName: this.data.storeList[e.detail.value].name,
      storeTip: false
    })
  },
  checkboxChange(e) {
    this.setData({
      isDefault: e.detail.value.length > 0
    })
  },
  confirm() {
    const {
      type
    } = this.data
    if (type == 'new') {
      this._confirmShop()
    } else {
      this._confirmTeacher()
    }
  },
  _confirmShop() {
    const {
      consignee,
      phone,
      region,
      point,
      address,
      isDefault,
      disabled
    } = this.data
    if (consignee && phone && mobileValidate(phone) && region.length > 0 && address && !disabled) {
      this.setData({
        disabled: true
      })
      addressAdd({
        consignee,
        phone,
        province: region[0],
        city: region[1],
        area: region[2],
        isDefault: isDefault ? 1 : 0,
        address
      }).then(() => {
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      }).catch(() => {
        this.setData({
          disabled: false
        })
      })
    } else {
      this.setData({
        consigneeTip: !consignee,
        phoneTip: !phone || !mobileValidate(phone),
        regionTip: region.length == 0,
        addressTip: !address
      })
    }
  },
  _confirmTeacher() {
    const {
      consignee,
      phone,
      region,
      point,
      address,
      disabled,
      isDefault,
      id
    } = this.data
    if (consignee && phone && mobileValidate(phone) && region.length > 0 && address && !disabled) {
      this.setData({
        disabled: true
      })
      addressUpdate({
        id,
        consignee,
        phone,
        province: region[0],
        city: region[1],
        area: region[2],
        address,
        isDefault: isDefault ? 1 : 0
      }).then(() => {
        wx.showToast({
          title: '操作成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      }).catch(() => {
        this.setData({
          disabled: false
        })
      })
    } else {
      this.setData({
        consigneeTip: !consignee,
        phoneTip: !phone || !mobileValidate(phone),
        regionTip: region.length == 0,
        addressTip: !address,
      })
    }
  },
  delAddr() {
    let that = this
    wx.showModal({
      title: '提示',
      content: '确认删除此收货地址吗？',
      success(res) {
        if (res.confirm) {
          addressDelete(that.data.id).then(() => {
            wx.showToast({
              title: '操作成功',
              icon: 'none'
            })
            setTimeout(() => {
              wx.navigateBack()
            }, 1500)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})